import java.io.File;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class Main extends UiAutomatorTestCase {
	private UiDevice mDevice = null;
	private static String Log_TAG = "FGO";
	private static String SCREEN_SHOP_PATH = "/data/local/tmp/ss/";
	
	private static HashMap<String, String> map = new HashMap<String, String>(){{
		put("MessageRow", "com.tencent.mm:id/ckb");//row
		put("MessageText", "com.tencent.mm:id/b4e");//text
		put("MessagePicCountText", "com.tencent.mm:id/cov");//pic count
		put("MessageDay", "com.tencent.mm:id/clw");//day
		put("MessageMonth", "com.tencent.mm:id/clx");//month
	}};
	
	protected void setUp() throws Exception{
		super.setUp();
		mDevice = getUiDevice();
	}
	
	public void testDemo(){
		Logd("init");
		UiScrollable list = new UiScrollable(new UiSelector().className("android.widget.ListView"));
		UiSelector rowSelector = new UiSelector().resourceId(map.get("MessageRow"));
		UiObject row = null;
		
		String strText = "";
		String strDay = "";
		String strMonth = "";
		
		for(int page=0; page<10; page++){
			Loge("count:"+list.getChildCount(rowSelector));
			for(int i = 0; i<list.getChildCount(rowSelector); i++,i++){
				try {
					row = list.getChildByInstance(rowSelector, i);
				} catch (UiObjectNotFoundException e) {
					Logd(i+": ignore");
					continue;
				}

				try {
					strDay = (row.getChild(new UiSelector().resourceId(map.get("MessageDay")))).getText();
					strMonth = (row.getChild(new UiSelector().resourceId(map.get("MessageMonth")))).getText();
				} catch (UiObjectNotFoundException e) {}
				
				try {
					strText = (row.getChild(new UiSelector().resourceId(map.get("MessageText")))).getText();
				} catch (UiObjectNotFoundException e) {
					continue;
				}
				
				String strCount = "";
				try {
					strCount = (row.getChild(new UiSelector().resourceId(map.get("MessagePicCountText")))).getText();
				} catch (UiObjectNotFoundException e1) {}
				
				println(strMonth+"-"+strDay+"-"+strCount+"-"+strText);
			}
			
			pageUp();
		}
	}
	
	private void pageUp(){
		Logd("Page Up");
		int height = mDevice.getDisplayHeight();
		int width = mDevice.getDisplayWidth();
		mDevice.swipe(width/2, (int)(height * 0.8), width/2, (int)(height * 0.2), 30);
		sleep(500);
	}
	
	private void pageDown(){
		Logd("Page Down");
		int height = mDevice.getDisplayHeight();
		int width = mDevice.getDisplayWidth();
		mDevice.swipe(width/2, (int)(height * 0.3), width/2, (int)(height * 0.7), 30);
		sleep(500);
	}
	
	private void Logd(String str){
		Date now = new Date();
		android.util.Log.d(Log_TAG + "[" + now.toGMTString() + "] ", str);
	}
	
	private void Loge(String str){
		Date now = new Date();
		android.util.Log.e(Log_TAG + "[" + now.toGMTString() + "] ", str);
		takeScreenShop();
	}
	
	private void println(String s){
		s = URLEncoder.encode(s);
		System.out.println(s);
	}
	
	public void takeScreenShop(){
		mDevice.takeScreenshot(new File(SCREEN_SHOP_PATH+(new Date()).getTime()+".png"));
	}
}
